﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ganaye.EmbeddedDb
{
    public abstract class Client : Record
    {
        [Column(PrimaryKey = true)]
        public abstract int ClientCode { get; set; }
        public abstract string Name { get; set; }
        public abstract DateTime? Dob { get; set; }
        //public abstract Relationship<Order> Orders { get; }
        //[Index("mColumnName")]
        //public abstract Index<Client> ByName { get; }
    }

    public enum FieldState : byte
    {
        None,
        Read,
        Modified
    }

    public class ClientProxy : Client
    {
        //private readonly static RecordColumn ClientCodeColumn;
        //private readonly static RecordColumn NameColumn;
        //private readonly static RecordColumn DobColumn;

        private int mClientCode;
        private string mName;
        private DateTime? mDob;
        private bool mModified;

        static ClientProxy()
        {
            //ClientCodeColumn = new RecordColumn(typeof(Client),0);
            //NameColumn = new RecordColumn(typeof(Client),1);
            //DobColumn = new RecordColumn(typeof(Client), 2);
        }

        public ClientProxy()
        {
            mFieldStates = new FieldState[3]; // 3 fields
        }

        public override int ClientCode
        {
            get
            {
                ((TableColumnInt32)mTable.Columns[0]).ReadBufferValue(this, ref mClientCode);
                return mClientCode;
            }
            set
            {
                ((TableColumnInt32)mTable.Columns[0]).ChangeCurrentValue(this, ref mClientCode, value);
            }
        }

        public override string Name
        {
            get
            {
                ((TableColumn<String>)mTable.Columns[1]).ReadBufferValue(this, ref mName);
                return mName;
            }
            set
            {
                ((TableColumn<String>)mTable.Columns[1]).ChangeCurrentValue(this, ref mName, value);
            }
        }

        public override DateTime? Dob
        {
            get
            {
                ((TableColumn<DateTime?>)mTable.Columns[2]).ReadBufferValue(this, ref mDob);
                return mDob;
            }
            set
            {
                ((TableColumn<DateTime?>)mTable.Columns[2]).ChangeCurrentValue(this, ref mDob, value);
            }
        }



        public override byte[] GetReadBuffer()
        {
            throw new NotImplementedException();
        }
    }


    public abstract class Order : Record
    {
     //   [RecordColumn(PrimaryKey = true)]
        public abstract int OrderId { get; set; }
        public abstract double Total { get; set; }
     //   public virtual Client Client { get; set; }
     //   public abstract Relationship<OrderLine> Lines { get; }
    }

    public abstract class BadRecord : Record
    {
        public abstract string SOMEFIELD1 { get; set; }
        public abstract string SOMEFIELD2 { get; set; }
    }

    //public class OrderLine : Record
    //{
    //    [RecordColumn(PrimaryKey = true)]
    //    public virtual Order Order { get; set; }
    //    [RecordColumn(PrimaryKey = true)]
    //    public virtual int LineNo { get; set; }
    //    public virtual Product Product { get; set; }
    //    public virtual double Price { get; set; }
    //    public virtual int Qty { get; set; }
    //}

    //public class Product : Record
    //{
    //    [RecordColumn(PrimaryKey = true)]
    //    public virtual int ProductId { get; set; }
    //    public virtual int mColumnName { get; set; }
    //    public virtual double Price { get; set; }
    //}

}
